define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search:false,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'merid', title: __('Merid')},
                        // {field: 'group.name', title: __('Group'), visible: false},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'recommend.mobile', title: __('Recommend'), operate: 'LIKE'},
                        {field: 'recommend_count', title: '推荐人数', operate: false, sortable: true},
                        {field: 'avatar', title: __('Avatar'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'level', title: __('Level'), visible: false, searchList: {0:'普通会员',1:'报单中心',2:'代理商',3:'分公司',4:'运营中心'}},
                        {field: 'level_text', title: __('Level'), operate: false, sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {0:'男',1:'女'}},
                        {field: 'gender_text', title: __('Gender'), operate: false},
                        {field: 'balance', title: __('Balance'), operate: 'BETWEEN', sortable: true},
                        {field: 'integral', title: __('Integral'), operate: 'BETWEEN', sortable: true},
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), visible:false, searchList: {1:'正常', 0:'禁用'}},
                        {field: 'status_text', title: __('Status'), operate:false},
                        {field: 'is_buy_cart', title: '是否入单', visible:false, searchList: {1:'是', 0:'否'}},
                        {field: 'is_buy_cart_text', title: '是否入单', operate:false},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                
                                {
                                    name: 'vip', 
                                    text: 'VIP设置', 
                                    title: 'VIP设置', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-warning btn-dialog', 
                                    url: 'user/user/vip', 
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'password', 
                                    text: '修改密码', 
                                    title: '修改密码', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-dialog', 
                                    url: 'user/user/password', 
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'info', 
                                    text: '', 
                                    title: '查看', 
                                    icon: 'fa fa-eye', 
                                    classname: 'btn btn-xs btn-success btn-dialog', 
                                    url: 'user/user/info', 
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'recharge', 
                                    text: '充值', 
                                    title: '充值', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-dialog', 
                                    url: 'user/user/recharge', 
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'changerecommend', 
                                    text: '更改上级', 
                                    title: '更改上级', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-dialog', 
                                    url: 'user/user/changeRecommend', 
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'changeagent', 
                                    text: '更改代理', 
                                    title: '更改代理', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-dialog', 
                                    url: 'user/user/changeAgent', 
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                       
                                $(table).data("operate-del", null);
                                if(row.recommend_count > 0) {
                                    $(table).data('operate-changerecommend', null);
                                }
                                // $(table).data("operate-recommend", null);
                                // $(table).data("operate-agent", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        changerecommend: function () {
            Controller.api.bindevent();
        },
        changeagent: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        vip: function () {
            Controller.api.bindevent();
        },
        password: function () {
            Controller.api.bindevent();
        },
        info: function () {
            Controller.api.bindevent();
        },
        recharge: function () {
            Controller.api.bindevent();
        },
        
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
    
});